<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\AdminUser;
use Validator;

class Admin_UserController extends Controller
{
    //声明全局变量
    public $res;
    /**
     * 构造Model类 进行实例化操作
     * Admin_UserController constructor.
     */
    public function __construct()
    {
        $this->res = new AdminUser();
    }

    /**
     *管理员页面展示
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       $data = $this->res->getUser();
       return view('admin.admin_user',['data'=>$data]);
    }

    /**
     * 添加管理员添加页面展示
     * Show the form for creating a new resource.
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.admin_add');
    }

    /**
     * 管理员添加操作
     * Store a newly created resource in storage.
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //错误提示
        $messages = [
            'name.required'    		 =>  '名字不能为空',
            'password.required'       =>  '密码不能为空',
            'pwd.required'      		 =>  ' 请重新确认密码',
            'unique.required'         => '用户名已存在',
            'min.required'             => '密码至少四位',
        ];
        //对啊数值进行限制
       $this-> validate( $request,[
            'name' 		=>  'required|unique:admin_user|max:20',
            'password' =>  'required|min:4',
            'pwd'      =>  'required',
        ],$messages);
        //把接收的值付给数组
        $user['name'] = $request->name;
        $user['password']=Md5($request->password);
        //判断数据插入数据是否成功
        if($this->res->insets($user)){
            return redirect('/admin/adminUser');
        }else{
            back();
        }
    }

    /**
    *
     * Display the specified resource.
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * 管理员编辑页面展示
     * Show the form for editing the specified resource.
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = $this->res->finds($id);
        return view('admin.admin_edit',['data'=>$data]);
    }

    /**
     *管理员信息更改操作
     * Update the specified resource in storage.
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data['name'] = $request->name;
        if($request->password == $request->pwd){
            $data['password'] = md5($request->password);
            $this->res->updates($id,$data);
            return redirect('/admin/adminUser');
        }else{
            return back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 后台登录页面展示
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getLogin()
    {
        return view('admin.admin_login');
    }

    /**
     * 后台登录判断信息操作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postLogin(Request $request)
    {
        //获取from提交的信息
        $info[name] = $request->name;
        $info[password] = $request->password;
        //和数据库数据进行对比
        $data = $this->res->postGet($info);
        //$data = DB::table('control')->where(['uname'=>$name, 'password'=>$password])->get();
        //判断信息是否为空
        if(empty($data)){
            //空，返回登录界面
            return redirect("/admin/login");
        }else {
            //不空，进入主页
            return redirect("/admin/index");
        }
    }

    /**
     * 管理员删除操作
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request)
    {
        $id = $request->id;
        return $this->res->deletes($id);
    }

}
